package edu.pku.sei.transformation.editor.controller.editparts.node;

import java.beans.PropertyChangeEvent;
import java.util.ArrayList;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;

import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.transformation.editor.model.AtomicTransformation;
import edu.pku.sei.transformation.editor.model.Key;
import edu.pku.sei.transformation.editor.model.ModelPackage;
import edu.pku.sei.transformation.editor.model.Rule;
import edu.pku.sei.transformation.editor.model.Transformation;
import edu.pku.sei.transformation.editor.views.figures.TransformationFigure;
import edu.pku.sei.transformation.editor.views.icons.IconsProvider;

public class AtomicTransformationEditpart extends TransformationEditpart {

	public AtomicTransformationEditpart(MetaShapeElement model) {
		super(model);
	}

	

	@Override
	protected IFigure createFigure() {
		TransformationFigure figure =  new TransformationFigure();
		AtomicTransformation trans = (AtomicTransformation) getNode().getModelElement();
		figure.setName(trans.getName());
		figure.setTag("<<Atomic Transformation>>");
		
		ArrayList<Label> list = new ArrayList<Label>();
		
		for (Object obj : trans.getKey()) {
			if (obj instanceof Key) {
				Label label = new Label(obj.toString());
				label.setIcon(IconsProvider.getImage(IconsProvider.KEY_ICO));
				list.add(label);
			}
		}
		
		for (Object obj : trans.getRules()) {
			if (obj instanceof Rule) {
				Label label = new Label(((Rule)obj).getName());
				label.setIcon(IconsProvider.getImage(IconsProvider.RULE_ICO));
				list.add(label);
			}
		}
		
		figure.setBody(list);

		return figure;
	}
	
	
	@Override
	protected void handlePropertyChanged(PropertyChangeEvent event) {
		Transformation trans = (Transformation) getNode().getModelElement();
		TransformationFigure figure = (TransformationFigure) getFigure();
		String propertyName = event.getPropertyName();
		
		if (ModelPackage.TRANSFORMATION_KEY.equals(propertyName)
				|| ModelPackage.TRANSFORMATION_RULES.equals(propertyName)
				|| ModelPackage.NAMEDELEMENT_NAME.equals(propertyName)) {
			refreshBody();
		}
		else {
			super.handlePropertyChanged(event);
		}
	}
	
	
	
	public void refreshBody() {
		AtomicTransformation trans = (AtomicTransformation) getNode().getModelElement();
		TransformationFigure figure = (TransformationFigure) getFigure();
		ArrayList<Label> list = new ArrayList<Label>();
		
		for (Object obj : trans.getKey()) {
			if (obj instanceof Key) {
				Label label = new Label(obj.toString());
				label.setIcon(IconsProvider.getImage(IconsProvider.KEY_ICO));
				list.add(label);
			}
		}
		
		for (Object obj : trans.getRules()) {
			if (obj instanceof Rule) {
				Label label = new Label(((Rule)obj).getName());
				label.setIcon(IconsProvider.getImage(IconsProvider.RULE_ICO));
				list.add(label);
			}
		}
		
		figure.setBody(list);
	}
	
}
